package com.aliyun.messagemgt.web.controller;

import com.aliyun.messagemgt.application.service.BaseTenantService;
import com.aliyun.messagemgt.common.dto.foura.FouraUserInfoDTO;
import com.aliyun.messagemgt.common.dto.foura.FouraUserLoginQry;
import com.aliyun.messagemgt.common.dto.response.ResultMap;
import com.aliyun.messagemgt.dto.BaseTenantDto;
import com.aliyun.messagemgt.entities.BaseTenant;
import com.aliyun.messagemgt.fouraplugin.FouraService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/base/tenant")
@Api(tags = "租户用户")
public class BaseTenantController {

    @Autowired
    private BaseTenantService baseTenantService;

    @Autowired
    private FouraService fouraService;

    @RequestMapping("/selectByTenantCode")
    public Object selectByTenantCode(@RequestParam("tenantCode") String tenantCode) {
        BaseTenant baseTenant = baseTenantService.selectByTenantCode(tenantCode);
        return baseTenant;
    }

    @PostMapping("/updateByTenantCode")
    public Object updateByTenantCode(@RequestBody BaseTenantDto baseTenant) {
        BaseTenant tenant = new BaseTenant();
        BeanUtils.copyProperties(baseTenant,tenant);
        return baseTenantService.updateByTenantCode(tenant);
    }

    @RequestMapping("/updateKeyName")
    public Object updateKeyName(@RequestParam("tenantCode") String tenantCode) {

        return baseTenantService.updateKeyCode(tenantCode);
    }

    @PostMapping("/checkLogin")
    public ResultMap<FouraUserInfoDTO> checkLogin(@RequestBody FouraUserLoginQry vo) {
        ResultMap resultMap = new ResultMap();
        if (vo.getPassWd() == null || vo.getPassWd() == "") {
            resultMap.setErrCode(1);
            resultMap.setErrMsg("密码不可以为空");
            return resultMap;
        }
        FouraUserInfoDTO fouraUserInfoDTO = fouraService.checkLogin(vo);
        if (fouraUserInfoDTO == null) {
            resultMap.setErrCode(1);
            resultMap.setErrMsg("密码错误！");
            return resultMap;
        } else {
            resultMap.setErrCode(0);
            resultMap.setErrMsg("验证成功！");
            resultMap.setData(fouraUserInfoDTO);
        }
        return resultMap;
    }

}
